package com.longway.common.kit;

import java.util.List;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
/**
 * 常用工具类
 */
public class CommonKit {
	
	public static int[] levelSetLRChilds(int levelCode ,int rightCode,String id,String table,String pkid,String pranrentid){
		int zs = 0;
		zs= Zcount(zs,id,table,pranrentid);
		int rCode = (zs*2)+1 + levelCode;
		Db.update("update "+table+" set leftCode = ? ,rightCode = ? where "+pkid+" = ?",levelCode,rCode,id);
		List<Record> list = Db.find("select * from "+table+" where "+pranrentid+" = ? ",id);
		for(int i=0;i<list.size();i++){
			Record re = list.get(i);
			String eid = re.getStr("id");
			if(i==0){
				levelCode = levelCode +1;
			}else{
				levelCode = rightCode + 1;
			}
			int[] rl = levelSetLRChilds(levelCode ,rightCode,eid,table,pkid,pranrentid);
			levelCode = rl[0];
			rightCode = rl[1];
		}
		int[] result ={levelCode,rCode};
		return result;
	}
	
	public static int Zcount(int zs ,String id,String table,String pranrentid){
		List<Record> list = Db.find("select * from "+table+" where "+pranrentid+" = ? ",id);
		zs = zs + list.size();
		for(Record re : list){
			zs = Zcount(zs ,re.getStr("id"),table,pranrentid);
		}
		return zs;
	}
}
		
		
	
	


